package com.wuming.house.api;

import com.wuming.house.core.BusinessException;
import com.wuming.house.core.HouseType;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.*;
import com.wuming.house.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页的接口
 */
@Controller
@RequestMapping("/api/house")
public class HouseApi {

    private static Logger logger = LoggerFactory.getLogger(HouseApi.class);

    @Autowired
    private HouseImgService houseImgService;

    @Autowired
    private CateHouseService cateHouseService;

    @Autowired
    private HouseCommentService houseCommentService;

    @Autowired
    private HouseTagService houseTagService;

    /**
     * 根据分类查询房产信息
     *
     * @param cateId 分类id
     * @return 分组房产信息
     */
    @ResponseBody
    @RequestMapping(value = "/cate/house/{cateId}", method = RequestMethod.GET)
    public JsonResult getHouseByCate(@PathVariable Integer cateId, @RequestParam(required = false) String houseName, @RequestParam(required = false) String area, @RequestParam(required = false) String price, @RequestParam(required = false) String houseLayout, @RequestParam(defaultValue = "1") int pageIndex,
                                     @RequestParam(defaultValue = "10") int pageSize) {
        try {
            //查询分类房产信息
            List<CateHouse> cateHouses = cateHouseService.getHouseByParams(cateId, houseName, area, price, houseLayout, pageIndex, pageSize);
            int total = cateHouseService.getHouseTotal(cateId, houseName, area, price, houseLayout);
            for (CateHouse ch : cateHouses) {
                House house = ch.getHouse();
                List<HouseComment> comments = houseCommentService.selectHouseCommentsByPage(null, null, house.getId(), null,1, 99999);
                if (null != comments && !comments.isEmpty()) {
                    house.setCommentNum(comments.size());
                    house.setScore(houseCommentService.getScoreByHouseComments(comments));
                } else {
                    house.setCommentNum(0);
                    house.setScore(0);
                }
                List<HouseTag> houseTags = houseTagService.getHouseTagByHouseId(house.getId(), HouseType.NEW_HOUSE.getValue().intValue());
                house.setHouseTags(houseTags);
            }
            return new JsonResult(cateHouses, "查询分类房产信息成功.", true,total);
        } catch (BusinessException be) {
            logger.error("查询分类房产信息失败,异常:{}", be.getMessage());
            return new JsonResult(null, "查询分类房产信息失败.", false);
        } catch (Exception e) {
            logger.error("查询分类房产信息失败,异常:{}", e.getMessage());
            return new JsonResult(null, "查询分类房产信息失败.", false);
        }
    }

    /**
     * 根据楼盘id查询楼盘图片
     *
     * @param houseId 楼盘id
     * @return 楼盘图片
     */
    @ResponseBody
    @RequestMapping(value = "/house/img/{houseId}", method = RequestMethod.GET)
    public JsonResult getHouseImgById(@PathVariable Long houseId, @RequestParam(defaultValue = "1") Integer houseType, @RequestParam(defaultValue = "1") int pageIndex,
                                      @RequestParam(defaultValue = "20") int pageSize) {
        try {
            List<HouseImg> houseImgs = houseImgService.getHouseImgByHouseId(houseId, houseType, pageIndex, pageSize);
            return new JsonResult(houseImgs, "查询楼盘图片信息成功.", true);
        } catch (BusinessException be) {
            logger.error("查询楼盘图片信息失败,异常:{}", be.getMessage());
            return new JsonResult(null, "查询楼盘图片信息失败.", false);
        } catch (Exception e) {
            logger.error("查询楼盘图片信息失败,异常:{}", e.getMessage());
            return new JsonResult(null, "查询楼盘图片信息失败.", false);
        }
    }
}
